﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using com.code.Editors.Code;

namespace com.Editors.UserControls
{
    public partial class ucTableRelation : UserControl
    {
        ArrayList tables = null;
        private bool isFirst = false;

        public ucTableRelation()
        {
            InitializeComponent();
        }

        public void setDataTables(ArrayList list)
        {
            tables = list;
            foreach (GuidDataTable table in tables)
                this.BindTables(table);
            this.BindJoins(cmbJoin);
        }

        private void BindTables(GuidDataTable table)
        {
            cmbParentTables.Items.Add(table.TableName);
            cmbChildTable.Items.Add(table.TableName);
            
        }

        private void cmbParentTables_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.ParentTable = cmbParentTables.Text;
            cmbParentTableCols.Items.Clear();
            foreach (GuidDataTable table in tables)
            {
                if (table.TableName == cmbParentTables.Text)
                {
                    foreach(GuidDataColumn col in table.ListColumns)
                        cmbParentTableCols.Items.Add(col.ColName); 
                }
            }
            if (!IsFirst)
            {
                cmbChildTable.Items.Clear();
                ArrayList tmpList = ((UITableRelation)this.ParentForm).GetSelectedTables();
                foreach (string tbl in tmpList)
                    cmbChildTable.Items.Add(tbl);
            }
        }

        private void cmbChildTable_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.ChildTable = cmbChildTable.Text;
            cmbChildCol.Items.Clear();
            foreach (GuidDataTable table in tables)
            {
                if (table.TableName == cmbChildTable.Text)
                {
                    foreach(GuidDataColumn col in table.ListColumns)
                        cmbChildCol.Items.Add(col.ColName);
                }
            }
        }

        private void BindJoins(ComboBox c)
        {
            if (IsFirst)
            {
                c.Items.Add("INNER JOIN");
                c.Items.Add("OUTER JOIN");
                c.Items.Add("LEFT JOIN");
                c.Items.Add("CROSS JOIN");
            }
            else
            {
                c.Items.Add("ON");
            }
           
           c.SelectedIndex = 0;
        }


        public bool IsFirst
        {
            get { return isFirst; }
            set { isFirst = value; }
        }
        private string parentTable;

        public string ParentTable
        {
            get { return parentTable; }
            set { parentTable = value; }
        }
        private string parentCol;

        public string ParentCol
        {
            get { return parentCol; }
            set { parentCol = value; }
        }
        private string joinType;

        public string JoinType
        {
            get {
                if (joinType == null) return cmbJoin.Text;
                else return joinType; }
            set { joinType = value; }
        }
        private string childTable;

        public string ChildTable
        {
            get { return childTable; }
            set { childTable = value; }
        }
        private string childCol;

        public string ChildCol
        {
            get { return childCol; }
            set { childCol = value; }
        }

        private void cmbJoin_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.JoinType = cmbJoin.Text;
        }

        private void cmbParentTableCols_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.ParentCol = cmbParentTableCols.Text;
        }

        private void cmbChildCol_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.ChildCol = cmbChildCol.Text;
        }

    }
}
